package com.sj.purchase.util;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.KeyStore;

/**
 * Created by 神盾局 on 2017/3/2.
 */
public class HttpUtil {

    private static CloseableHttpClient httpclient;
    private static CloseableHttpClient httpsclient;

    public static CloseableHttpClient httpclient() {
        httpclient = HttpClients.createDefault();
        return httpclient;
    }

    public static CloseableHttpClient httpsclient(String mch_id) {
        try {
            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            //String certificatePath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "static/certificate/apiclient_cert.p12";
            //certificatePath = URLDecoder.decode(certificatePath, "utf-8");
            //LogUtil.info("certificatePath:" + certificatePath);
            FileInputStream instream = new FileInputStream(new File("/client.p12"));
            keyStore.load(instream, mch_id.toCharArray());
            try {
                instream.close();
            } catch (IOException ex) {
                LogUtil.error("发送https请求异常，异常信息：", ex.getMessage());
            }
            SSLContext sslcontext = SSLContexts.custom()
                    .loadKeyMaterial(keyStore, mch_id.toCharArray())
                    .build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                    sslcontext,
                    new String[]{"TLSv1"},
                    null,
                    SSLConnectionSocketFactory.getDefaultHostnameVerifier());
            httpsclient = HttpClients.custom()
                    .setSSLSocketFactory(sslsf)
                    .build();
        } catch (Exception e) {
            LogUtil.error("获取证书异常，异常信息:", e.getStackTrace());
        }
        return httpsclient;
    }

    public static void close(CloseableHttpClient client) {
        try {
            client.close();
        } catch (IOException ex) {
            LogUtil.error("关闭流失败", ex.getMessage());
        }
    }

    public static HttpPost post(String url) {
        HttpPost post = new HttpPost(url); // 设置响应头信息
        post.addHeader("Connection", "keep-alive");
        post.addHeader("Accept", "*/*");
        post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        post.addHeader("X-Requested-With", "XMLHttpRequest");
        post.addHeader("Cache-Control", "max-age=0");
        post.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
        return post;
    }

    public static HttpGet get(String url) {
        HttpGet get = new HttpGet(url);
        get.addHeader("Connection", "keep-alive");
        get.addHeader("Cache-Control", "max-age=0");
        get.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
        get.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        return get;
    }

    public static String getIP() {
        String host = null;
        try {
            host = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            LogUtil.error("get server host Exception e:", e);
        }
        return host;
    }
}
